package siau.android.base

import android.Manifest
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Environment
import android.provider.MediaStore
import android.view.Gravity
import android.view.View
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.result.ActivityResultLauncher
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.FileProvider
import com.tbruyelle.rxpermissions3.RxPermissions
import hhm.android.library.getPictureOrTakePhoto.TakePhotoPopWindow
import hhm.android.library.netWorkState.NetWorkDialog
import hhm.android.library.utils.OnMultiClickListener
import siau.android.base.zxing.android.CaptureActivity
import java.io.File

class TakePhotoPopWindowUtils {
    companion object {
        private val picturePermissions = arrayOf(
            Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE
        )
        private val cameraPermissions = arrayOf(
            Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA
        )

        var mTempPhotoPath: String? = ""

        fun takePhoto(context: AppCompatActivity, anchorPoint: View, takePhotoResult: ActivityResultLauncher<Intent>, selectPicResult: ActivityResultLauncher<Intent>) {

            val takePhotoPopWin = TakePhotoPopWindow(context, object : OnMultiClickListener() {
                override fun onMultiClick(v: View?) {
                    val rxPermissions = RxPermissions(context)
                    rxPermissions.request(*cameraPermissions).subscribe {
                        if (it) {
                            val intentToTakePhoto = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
                            val fileDir: File = if (Build.VERSION.SDK_INT >= 30) {
                                File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)?.absolutePath + File.separator + "photoTest" + File.separator)
                            } else {
                                File(Environment.getExternalStorageDirectory().toString() + File.separator + "photoTest" + File.separator)
                            }
                            if (!fileDir.exists()) {
                                fileDir.mkdirs()
                            }

                            val photoFile = File(fileDir, "photo.jpeg")
                            mTempPhotoPath = photoFile.absolutePath
                            val imageUri = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                                FileProvider.getUriForFile(context, "com.siau.SevenBubble.fileprovider", photoFile)
                            } else {
                                Uri.fromFile(photoFile)
                            }
                            intentToTakePhoto.putExtra(MediaStore.EXTRA_OUTPUT, imageUri)
                            takePhotoResult.launch(intentToTakePhoto)
                        } else {
                            Toast.makeText(context, "请先授予相机相关权限", Toast.LENGTH_SHORT).show()
                        }
                    }
                }
            }, object : OnMultiClickListener() {
                override fun onMultiClick(v: View?) {
                    val rxPermissions = RxPermissions(context)
                    rxPermissions.request(*picturePermissions).subscribe {
                        if (it) {
                            val intentToPickPic = Intent(Intent.ACTION_PICK, null)
                            intentToPickPic.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*")
                            selectPicResult.launch(intentToPickPic)
                        } else {
                            Toast.makeText(context, "请先授予相册相关权限", Toast.LENGTH_SHORT).show()
                        }
                    }
                }
            })
            takePhotoPopWin.showAtLocation(anchorPoint, Gravity.CENTER, 0, 0)
        }
    }
}